﻿using System.Net.Http.Headers;
using System.Text.Json.Nodes;
using iOSApp.Web.Layout;
using iOSClub.App.Share;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;

namespace iOSApp.Web.Pages;

public partial class Home
{
    [CascadingParameter] private MemberModel Member { get; set; } = new();

    private static string TaskColor(TaskModel task)
    {
        var today = DateTime.Today;
        var start = DateTime.Parse(task.StartTime);
        var end = DateTime.Parse(task.EndTime);
        if (today > end) return "red-inverse";
        if (start > today) return "blue-inverse";
        return task.Status ? "#87d068" : "#2db7f5";
    }

    private static string TaskString(TaskModel task)
    {
        var today = DateTime.Today;
        var start = DateTime.Parse(task.StartTime);
        var end = DateTime.Parse(task.EndTime);
        if (today > end) return "超时未完成";
        if (start > today) return "未开始";
        return task.Status ? "已完成" : "正在进行";
    }

    private List<ToolModel> Tools { get; set; } = [];
    private TaskModel[] Tasks { get; set; } = [];
    private ResourceModel[] Resources { get; set; } = [];
    private RssModel[] Entries { get; set; } = [];

    protected override async Task OnInitializedAsync()
    {
        var response = await HttpClient.GetAsync("http://localhost:5103/api/Member/GetAllTools");
        var json = await response.Content.ReadAsStringAsync();

        Tools = JsonConvert.DeserializeObject<List<ToolModel>>(json) ?? [];
        Tools.AddRange(
            [
                new ToolModel()
                {
                    Name = "图像解析",
                    Url = "/Tool/Converter/Base",
                    IconUrl = "Icon:"
                },
                new ToolModel()
                {
                    Name = "Base64文本解析",
                    Url = "https://github.com/iOSClub/iOSClub.App",
                    IconUrl = "Icon:folder_zip"
                },
                new ToolModel()
                {
                    Name = "GZip",
                    Url = "/Tool/Encode/GZip",
                    IconUrl = "Icon:folder_zip"
                }
            ]
        );

        json = await HttpClient.GetStringAsync("https://test.xauat.site/feeds/MP_WXS_3226711201.json");
        var a = JsonNode.Parse(json);

        if (a != null)
        {
            Entries = a["items"]?.AsArray()
                .Select(x => new RssModel()
                {
                    Url = x?["url"]?.GetValue<string>() ?? "",
                    Title = x?["title"]?.GetValue<string>() ?? "",
                    Image = x?["image"]?.GetValue<string>() ?? "",
                    DateTime = x?["date_modified"]?.GetValue<DateTime>() ?? DateTime.Today
                })
                .ToArray() ?? [];
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        var jwt = (JwtProvider)authStateProvider;
        var cookie = await jwt.GetCookie();

        if (string.IsNullOrEmpty(cookie)) return;

        Console.WriteLine(Member.Identity);

        if (Member.Identity != "Member" && Member.Identity != "Founder")
        {
            HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", cookie);

            var response = await HttpClient.GetAsync("http://localhost:5103/api/Project/GetResources");
            var json = await response.Content.ReadAsStringAsync();
            Resources = JsonConvert.DeserializeObject<ResourceModel[]>(json) ?? [];

            response = await HttpClient.GetAsync("http://localhost:5103/api/Project/GetYourTasks");
            json = await response.Content.ReadAsStringAsync();
            Tasks = JsonConvert.DeserializeObject<TaskModel[]>(json) ?? [];
        }

        await base.OnAfterRenderAsync(firstRender);
    }
}